﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace zmMoniter
{
    /// <summary>
    /// 操作设备
    /// </summary>
    class Devices
    {
        private static PublicDataCls cls = new PublicDataCls();

        private static ArrayList al = new ArrayList();
        /// <summary>
        /// 停止设备
        /// </summary>
        /// <param name="deviceID">设备ID</param>
        /// <returns></returns>
        public static void stopDevice(string deviceID)
        {
            al = new ArrayList();
            string sSQL = "UPDATE 振捣设备基本信息表 SET 仓面号=0,使用设备状态 = 0 WHERE 编号=" + deviceID;//设备停用
            al.Add(sSQL);
            sSQL = "UPDATE 振捣设备使用记录表 SET 结束时间=to_date('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
                           "','yyyy-mm-dd hh24:mi:ss') WHERE 设备编号=" + deviceID + " AND 振捣设备使用记录表.结束时间 IS NULL";//结束原始记录
            al.Add(sSQL);
            for (int i = 0; i < al.Count; i++)
            {
                cls.ExecuteSQL((string)al[i]);
            }
        }

        /// <summary>
        /// 启用设备
        /// </summary>
        /// <param name="deviceID">设备ID</param>
        /// <param name="iDeckID">仓号</param>
        public static void startDevice(string deviceID, int iDeckID)
        {
            al = new ArrayList();
            string sSQL = "UPDATE 振捣设备基本信息表 SET 仓面号=" + iDeckID + " WHERE 编号=" + deviceID;//更新设备振捣仓号
            al.Add(sSQL);
            sSQL = "UPDATE 振捣设备使用记录表 SET 结束时间=to_date('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
            "','yyyy-mm-dd hh24:mi:ss') WHERE 设备编号=" + deviceID + " AND 振捣设备使用记录表.结束时间 IS NULL";//结束原始记录
            al.Add(sSQL);
            string upDatePlanDevices = getPlanDevice(iDeckID);
            if (upDatePlanDevices == "")
            {
                upDatePlanDevices = deviceID;
            }
            else
            {
                upDatePlanDevices += "," + deviceID;
            }
            sSQL = "UPDATE 仓面信息 set 规划设备='" + upDatePlanDevices + "' where 编号=" + iDeckID;
            al.Add(sSQL);
            for (int i = 0; i < al.Count; i++)
            {
                cls.ExecuteSQL((string)al[i]);
            }
        }

        /// <summary>
        /// 获取规划设备
        /// </summary>
        /// <param name="cmCode">仓面号</param>
        private static string getPlanDevice(int iDeckID)
        {
            string sSQL = @"SELECT   规划设备 FROM  仓面信息 where 编号=" + iDeckID;
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            DataTable dt = db.ReturnDataTable(sSQL);
            if (dt == null || dt.Rows.Count == 0)
                return "";
            else
                return dt.Rows[0][0].ToString();
        }
    }
}
